import * as path from 'path';
import type { Plugin, ResolvedConfig } from 'vite';

export const importFullUiPlugin = () => {
	let config: ResolvedConfig;
	return <Plugin>{
		name: 'importFullElementPlugin',
		async configResolved(conf) {
			config = conf;
		},
		transform(code, id) {
			const mainPath = path.join(config.root, 'src/main.ts').split('\\').join('/');
			// 判断当前处理的是否是 src/main.ts
			if (mainPath === id) {
				const name = 'naive';
				// 引入 naive
				const prepend = `import ${name} from 'naive-ui';\n`;
				code = code.replace('.mount("#app")', $1 => `.use(${name})` + $1);
				return prepend + code;
			}
			return code;
		},
	};
};
